package com.sgb.kjwl.viewmodel.material;

import androidx.databinding.ObservableField;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.SearchMaterialEntity3;
import com.sgb.kjwl.model.entity.request.MaterialFilterJson3;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.MaterialRepertory;
import com.sgb.kjwl.view.ui.fragment.material.SelectAllMaterialListFragment3;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class SelectAllGoodsMaterialListModel3 extends BaseViewModel {
    public ObservableField<String> areaCode = new ObservableField<>("");
    private MaterialRepertory repertory;
    private SelectAllMaterialListFragment3 fragment1;
    public ObservableField<MaterialFilterJson3> materialFilterJson = new ObservableField<>(new MaterialFilterJson3());
    public ObservableField<Integer> type = new ObservableField<Integer>(0);
    private int page = 1;
    private int size = 10;

    public SelectAllGoodsMaterialListModel3(SelectAllMaterialListFragment3 fragment, MaterialRepertory repertory) {
        this.fragment1 = fragment;
        this.repertory = repertory;
        areaCode.set(repertory.getAreaCode());
    }

    public String getAreaNameModel() {
        return repertory.getAreaName();
    }

    public String getAreaCodeModel() {
        return repertory.getAreaCode();
    }

    public void getMaterialModel(boolean isLoadMore) {
        page = isLoadMore ? page + 1 : 1;
        materialFilterJson.get().setPage(page);
        materialFilterJson.get().setSize(size);
        materialFilterJson.get().setAreaCode(areaCode.get());
        if (materialFilterJson.get().getPriceSort() == 0) {
            materialFilterJson.get().setTimeSort(2);
        } else {
            materialFilterJson.get().setTimeSort(0);
        }
        if (type.get().intValue()==1){
            materialFilterJson.get().setPromoteStatus(2);
        }
        DialogHelper.showProgressDialog(fragment1.getContext(), null, "获取信数据中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.getMaterialList3(materialFilterJson.get()).subscribe(new Consumer<BaseEntity<ListResponse<List<SearchMaterialEntity3>>>>() {
            @Override
            public void accept(BaseEntity<ListResponse<List<SearchMaterialEntity3>>> listResponseBaseEntity) throws Exception {
                DialogHelper.dismissProgressDialog();
                if (listResponseBaseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                    fragment1.showList(listResponseBaseEntity.getData().getList(), isLoadMore);
                }else {
                    MToast.showToast(fragment1.getContext(),listResponseBaseEntity.getMsg());
                    fragment1.showList(new ArrayList<>(),isLoadMore);
                }
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
            }
        });
    }
}